﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace Dusk_Tile_Editor
{
    public class TileSelectionPanel : Panel
    {
        SGP.ManagedDirect3D D3D = SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager MTM = SGP.ManagedTextureManager.Instance;

        private Point selectedTile;

        public string defaultBitmapFileName;

        List<Point> TilePoints = new List<Point>();

        public Tile[,] Tiles;

        public Point SelectedTile
        {
            get { return selectedTile; }
            set { selectedTile = value; }
        }

        public TileSet tileSet = new TileSet();

        private int imageID = -1;
        public int ImageID
        {
            get { return imageID; }
            set { imageID = value; }
        }

        public TileSelectionPanel()
        {
            this.DoubleBuffered = true;                  
        }

        public void Initialize()
        {

            tileSet.TileSetBitMap = new Bitmap(Properties.Resources.TileSet);
            tileSet.TileSetSize = new Size(3,3);
            tileSet.TileSize = new Size(64, 64);
            Tiles = new Tile[tileSet.TileSetSize.Width, tileSet.TileSetSize.Height];
            imageID = MTM.LoadTexture("../../TileSet.png", 0);
            this.AutoScrollMinSize = tileSet.TileSetBitMap.Size;
            for( int i = 0; i < tileSet.TileSetSize.Width; i++ )
            {
                for( int j = 0; j < tileSet.TileSetSize.Height; j++ )
                {
                    TilePoints.Add( new Point( i,j ) );
                    Tiles[ i,j ] = new Tile();
                    Tiles[ i,j ].HasItem = Tile.eItem.ITEM_NO_ITEM;
                    Tiles[ i,j ].TileType = Tile.eTileType.TILE_GROUND;
                    
                }
            }
            defaultBitmapFileName = "TileSet.png";
        }

        public void UpdateTileSet(int newTileWidth, int newTileHeight, 
            int newTileSetWidth, int newTileSetHeight)
        {
            int prevWidth = tileSet.TileSetSize.Width;
            int prevHeight = tileSet.TileSetSize.Height;
            tileSet.TileSetSize = new Size(newTileSetWidth, newTileSetHeight);
            tileSet.TileSize = new Size(newTileWidth, newTileHeight);
            TilePoints.Clear();
            Tile[,] tempTileList = new Tile[newTileSetWidth,newTileSetHeight];
            for (int i = 0; i < tileSet.TileSetSize.Width; i++)
            {
                for (int j = 0; j < tileSet.TileSetSize.Height; j++)
                {
                    TilePoints.Add(new Point(i, j));
                    if (j < prevHeight && i < prevWidth)
                        tempTileList[i, j] = Tiles[i, j];
                    else
                    {
                        tempTileList[i, j] = new Tile();
                        tempTileList[i, j].HasItem  = Tile.eItem.ITEM_NO_ITEM;
                        tempTileList[i, j].TileType = Tile.eTileType.TILE_GROUND;
                    }
                }
            }
            Tiles = tempTileList;
            this.AutoScrollMinSize = new Size( tileSet.TileSize.Width * tileSet.TileSetSize.Width,tileSet.TileSize.Height * tileSet.TileSetSize.Height );
        }

        public void Render()
        {
            if (imageID != -1)
            {
                D3D.ChangeDisplayParam(this);
                Point offset = new Point(0, 0);
                offset.X += this.AutoScrollPosition.X;
                offset.Y += this.AutoScrollPosition.Y;

                D3D.Clear(this, Color.Black);
                D3D.DeviceBegin();
                D3D.SpriteBegin();

                MTM.Draw(imageID, this.ClientRectangle.Left + offset.X, this.ClientRectangle.Top + offset.Y, 1.0f, 1.0f, Rectangle.Empty, 0, 0, 0.0f, 0);

                D3D.Sprite.Flush();


                for (int i = 0; i < tileSet.TileSetSize.Width * tileSet.TileSetSize.Height; i++)
                {

                    D3D.DrawEmptyRect(new Rectangle(TilePoints[i].X * tileSet.TileSize.Width + offset.X,
                            TilePoints[i].Y * tileSet.TileSize.Height + offset.Y, tileSet.TileSize.Width,
                            tileSet.TileSize.Height), Color.Black);
                }

                D3D.DrawEmptyRect(new Rectangle(selectedTile.X * tileSet.TileSize.Width + offset.X,
                    selectedTile.Y * tileSet.TileSize.Height + offset.Y,
                    tileSet.TileSize.Width, tileSet.TileSize.Height), Color.Red);

                D3D.SpriteEnd();
                D3D.DeviceEnd();
                D3D.Present(this);
            }

        }

    }
}
